Chat Consultation
ChatConfig
For chat
consultations, the consultation data will include the ChatConfig
object
This config data will be used to initiate and handle the chat between the user and the doctor
import com.google.gson.annotations.SerializedName
data class ChatConfig(
val id: Int?,
@SerializedName("consultation_id") val consultationId: Int?,
@SerializedName("group_id") val groupId: String?,
@SerializedName("app_id") val appId: String?,
@SerializedName("chat_user_id") val chatUserId: String?,
@SerializedName("chat_user_token") val chatUserToken: String?
) {
companion object {
fun fromJson(json: Map<String, Any?>): ChatConfig {
return ChatConfig(
id = json["id"] as? Int,
consultationId = json["consultation_id"] as? Int,
groupId = json["group_id"] as? String,
appId = json["app_id"] as? String,
chatUserId = json["chat_user_id"] as? String,
chatUserToken = json["chat_user_token"] as? String
)
}
}
}
Initiating the chat service
First the chat service should be initiated using the AltibbiChat.init()
method with some of Consultation
data (appId,chatUserId,chatUserToken)
from the getConsultationInfo
import com.altibbi.telehealth.AltibbiChat
AltibbiChat.init(response.chatConfig!!.appId!!, context, response.chatConfig!!.chatUserId!!, response.chatConfig!!.chatUserToken!!)
Adding Event Listeners and Handlers
You have to add some handler functions to the chatService channel, and there are two types of handlers MyChannelHandler
.
Check the code below to see how the handlers should be declared and added to the chatService channel
MyChannelHandler
class MyChannelHandler(
private val activity: Activity,
val onChannelMessageReceived: (BaseMessage) -> Unit
) : SendBird.ChannelHandler() {
override fun onMessageReceived(channel: BaseChannel, message: BaseMessage) {
if (message.message.isNotEmpty()) {
onChannelMessageReceived(message)
}
}
override fun onTypingStatusUpdated(channel: GroupChannel?) {
println("typing started from Dr side")
}
override fun onUserLeft(channel: GroupChannel?, user: User?) {
println("Chat finished")
activity.finish()
}
}
AltibbiChat.addChannelHandler("myChannelHandler",channelHandler)
Loading All Channel Messages
After initiate the AltibbiChat, you have to load the previous messages of the channel
From the ChatConfig
data, the groupId
represents the channelURL which is needed to load the previous messages
Check the code below:
note : I declared the currentChannel
as a global variable, assigned the channel value to it, and then used currentChannel directly in other places,
like currentChannel?.sendUserMessage.
var currentChannel: GroupChannel? = null
AltibbiChat.getChannel("channel_${response.chatConfig!!.groupId}", object :
AltibbiChat.Companion.ChannelCallback {
override fun onChannelReceived(channel: GroupChannel?) {
currentChannel = channel
currentChannel?.createPreviousMessageListQuery()?.load(100, false, object : MessageListQueryResult{
override fun onResult(
p0: MutableList<BaseMessage>?,
p1: SendBirdException?
) {
p0?.let { messageAdapter.addMessages(it) }
}
})
}
})
Sending Messages
Now the AltibbiChat is initiated, connected and started.
So you can start sending messages using sendUserMessage
from the AltibbiChat
with the message object that includes a message
attribute
Check the code below:
const message = "Message example"
currentChannel?.sendUserMessage(message, object : BaseChannel.SendUserMessageHandler {
override fun onSent(userMessage: UserMessage?, e: SendBirdException?) {
if (e == null) {
// message is sent
} else {
println("Error sending message: ${e.message}")
}
}
})
Sending media files (Pictures or PDFs)
You can send pictures and PDF files on the chat using the previously mentioned uploadMedia
functionality
The process could be done using the uploadMedia
and getting the url
from the response and send it just as a text